<template>
	<view class="page">
		<uni-forms ref="baseForm">
			<uni-forms-item>
				<view class="">1.锁具检查:查看锁是否无缺失、损坏</view>
				<uni-data-checkbox v-model="FormData.suoju" :localdata="rItems" />
			</uni-forms-item>
			<uni-forms-item>
				<view class="">2.风扇运行情况检查：查看个风扇运行是否正常</view>
				<uni-data-checkbox v-model="FormData.fengshan" :localdata="rItems" />
			</uni-forms-item>
			<uni-forms-item>
				<view class="">3.照明检测：下发指令检查灯管照明是否正常</view>
				<uni-data-checkbox v-model="FormData.zhaoming" :localdata="rItems" />
			</uni-forms-item>
			<uni-forms-item>
				<view class="">4.开关漏电保护器试验：实验按钮检测开关是否正常</view>
				<uni-data-checkbox v-model="FormData.kaiguan" :localdata="rItems" />
			</uni-forms-item>
			<uni-forms-item>
				<view class="">5.各螺栓端子紧固 ： 停电后对各线束端子进行紧固，线束接头进行检查</view>
				<uni-data-checkbox v-model="FormData.luoshuan" :localdata="rItems" />
			</uni-forms-item>
			<uni-forms-item>
				<view class="">6.候车厅项目：打开门对门框合页进行检查，灯箱固定螺栓、座椅固定螺栓的检查</view>
				<uni-data-checkbox v-model="FormData.houcheting" :localdata="rItems" />
			</uni-forms-item>
			<uni-forms-item>
				<view class="">7.屏前卫生清洁：屏前卫生打扫</view>
				<uni-data-checkbox v-model="FormData.weisheng" :localdata="rItems" />
			</uni-forms-item>
			<uni-forms-item>
				<view class="">8.箱体外观：查看箱体外观是否有掉漆生锈，外观是否完好</view>
				<uni-data-checkbox v-model="FormData.waiguan" :localdata="rItems" />
			</uni-forms-item>
			<uni-forms-item>
				<view class="">9.玻璃结构胶情况（站牌、候车厅）：查看固定玻璃的胶是否无开裂现象</view>
				<uni-data-checkbox v-model="FormData.jiegoujiao" :localdata="rItems" />
			</uni-forms-item>
			<uni-forms-item>
				<view class="">10.防尘棉更换：更换防尘棉</view>
				<uni-data-checkbox v-model="FormData.fangchenmian" :localdata="rItems" />
			</uni-forms-item>
			<uni-forms-item>
				<view class="">11.箱体内的卫生是否清洁：断电后吹风机除尘,无电的设备抚尘，箱体内的杂物清理</view>
				<uni-data-checkbox v-model="FormData.xiangnei" :localdata="rItems" />
			</uni-forms-item>
			<uni-forms-item>
				<view class="">12.维保人员</view>
				<!-- 	<uni-easyinput type="text" v-model="peoples" placeholder="请输入处理人" /> -->
				<view @click="control = true">
					<uni-easyinput placeholder="请选择处理人" v-model="people" disabled />
				</view>
				<z-multi-choose activeColor="red" idKey="realname" showKey="realname" :list="users" :show="control"
					@maskTouch="cancle_p" @sure="comform_p" @cancle="cancle_p"></z-multi-choose>
			</uni-forms-item>
			<uni-forms-item>
				<view class="">13.工控机类型</view>
				<uni-data-select v-model="FormData.armType" :localdata="arm_list"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item>
				<view class="">14.控制器类型</view>
				<uni-data-select v-model="FormData.controlType" :localdata="control_list"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item>
				<view class="">13.其他</view>
				<uni-easyinput type="textarea" v-model="FormData.note" placeholder="请输入内容"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item>
				<view class="">14.图片</view>
				<view class="u-demo-block__content">
					<view class="u-page__upload-item">
						<u-upload :fileList="pics1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
							:maxCount="9"></u-upload>
					</view>
				</view>
			</uni-forms-item>
			<uni-forms-item>
				<view class="">15.录像</view>
				<!-- <uni-file-picker v-model="FormData.video" limit="9" title="最多选择9张图片"
					@select="select2"></uni-file-picker> -->
						<u-upload :fileList="pics2" @afterRead="afterRead" @delete="deletePic" name="2" multiple accept="video"
							:maxCount="2"></u-upload>
	
			</uni-forms-item>
			<!-- <uni-forms-item label="年龄" required>
								<uni-easyinput v-model="FormData.age" placeholder="请输入年龄" />
							</uni-forms-item> -->
		</uni-forms>
		<u-button class="btn" @click="submit" type="primary" text="提交" />
	</view>
</template>

<script>
	import {
		baseURL
	} from '@/api/env.js';
	import {
		addMaintenance
	} from '@/api/common.js';
	import {
		findSysUserAll
	} from '@/api/common.js';
	import {
		getTreeJsonByCode
	} from '@/api/common.js';
	export default {
		data() {
			return {
				people: '',
				peopleArray: [],
				pics1: [], //用于图片回显
				pics2: [], //用于图片回显
				img1: [], //上传时数据处理
				img2: [],
				users: [],
				arm_list: [],
				control_list: [],
				control: false,
				FormData: {
					suoju: 1,
					suojuRemark: '',
					fengshan: 0,
					fengshanRemark: '',
					zhaoming: 0,
					zhaomingRemark: '',
					kaiguan: 0,
					kaiguanRemark: '',
					luoshuan: 0,
					luoshuanRemark: '',
					houcheting: 0,
					houchetingRemark: '',
					weisheng: 0,
					weishengRemark: '',
					waiguan: 0,
					waiguanRemark: '',
					jiegoujiao: 0,
					jiegoujiaoRemark: '',
					fangchenmian: 0,
					fangchenmianRemark: '',
					xiangnei: 0,
					xiangneiRemark: '',
					peoples: ['shadmin'],
					jingdu: '',
					weidu: '',
					note: '',
					sid: '',
					pid: '',
					pname: '',
					sname: '',
					dir: ''
				},
				rItems: [{
					text: '是',
					value: 1
				}, {
					text: '否',
					value: 0
				}],
				id: '',
				dataList: [],
				info: {}
			}
		},
		onLoad: function(option) {
			console.log("onLoad:" + option.info)
			this.info = JSON.parse(option.info)
			this.FormData.pname = this.info.pname
			this.FormData.pid = this.info.pid
			this.FormData.sid = this.info.id
			this.findSysUserAll();
			this.arm_types();
			this.control_types();
		},
		methods: {
			control_types() {
				getTreeJsonByCode({
					code: "ControlType"
				}).then(res => {
					console.log(res);
					if (res.length > 0) {
						this.jieguo_list = []
						res.forEach(item => {
							//forEach循环
							this.control_list.push({
								value: item.id,
								text: item.value
							})
						});
					}
				}).catch(err => {
					console.log(err);
					uni.showToast({
						title: err,
						icon: 'none'
					});
				})
			},
			arm_types() {
				getTreeJsonByCode({
					code: "ArmType"
				}).then(res => {
					console.log(res);
					if (res.length > 0) {
						this.jieguo_list = []
						res.forEach(item => {
							//forEach循环
							this.arm_list.push({
								value: item.id,
								text: item.value
							})
						});
					}
				}).catch(err => {
					console.log(err);
					uni.showToast({
						title: err,
						icon: 'none'
					});
				})
			},
			cancle_p() {
				this.control = false
				console.log("关闭弹窗:" + this.control)
			},
			comform_p(e) {
				console.log(JSON.stringify(e))
				this.control = false
				this.peopleArray = e.idArr //textArr
				this.people = JSON.stringify(e.textArr)
			},
			// 删除图片
			deletePic(event) {
				this[`pics${event.name}`].splice(event.index, 1)
				this[`img${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				console.log(event)
				let lists = [].concat(event.file)
				let fileListLen = this[`pics${event.name}`].length
				lists.map((item) => {
					this[`pics${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
					this[`img${event.name}`].push('')
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log('上传结果：' + result)
					let item = this[`pics${event.name}`][fileListLen]
					this[`pics${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					this[`img${event.name}`].splice(fileListLen, 1, result)
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					var token = "";
					try {
						token = uni.getStorageSync('token')
					} catch (e) {}
					let a = uni.uploadFile({
						url: baseURL + '/file/ubatchUpload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'image',
						header: {
							Authorization: token
						},
						success: (res) => {
							var res1 = JSON.parse(res.data)
							console.log('结果：' + res.data)
							if (res1.flag) { //成功
								resolve(res1.data.id)
							}
						}
					});
				})
			},
			submit() {
				this.add()
			},
			//获取人员
			findSysUserAll() {
				findSysUserAll().then(res => {
					console.log(res);
					this.users = res
					// res.forEach(item => {
					// 	//forEach循环
					// 	this.users.push({
					// 		realname: item.realname,
					// 		id: item.id
					// 	})
					// });
					console.log(this.users)

				}).catch(err => {
					console.log(err);
					uni.showToast({
						title: err,
						icon: 'none'
					});
				})
			},
			add() {
				// this.FormData.peoples = JSON.stringify(this.peoples)
				this.FormData.pic = JSON.stringify(this.img1)
				this.FormData.video = JSON.stringify(this.img2)
				this.FormData.peoples = this.peopleArray
				addMaintenance(
					this.FormData).then(res => {
					console.log('查詢結果：' + JSON.stringify(res));
					uni.showToast({
						title: "新增成功",
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateBack();
					}, 500)

				}).catch(err => {
					console.log(err);
					uni.showToast({
						title: err,
						icon: 'none'
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		margin-top: 32rpx;
		width: 710rpx;
		margin-left: 16rpx;
		margin-right: 16rpx;
	}

	/* .btn{
		background-color: $bg-color;
	} */
</style>